;;; This program is free software.  You may run, copy, modify, and
;;; redistribute it under the terms of the GNU General Purpose License
;;; (GPL) version 3, or any later version.

;;; Borrowed from (guix utils)

(define-module (derivative utils memoize)
  #:export (memoize))

(define (memoize proc)
  "Return a memoizing version of PROC"
  (let ((cache (make-hash-table)))
    (lambda args
      (let ((results (hash-ref cache args)))
	(if results
	    (apply values results)
	    (let ((results (call-with-values (lambda ()
					       (apply proc args))
			     list)))
	      (hash-set! cache args results)
	      (apply values results)))))))
